﻿package src.components 
{
	/**
	* 
	* Reliability project
	* -----------------------
	* @author  Dmitrij Boycov
	* @mail    dmitry.boycov@gmail.com
	* @date    April 27, 2010
	* @version 1
	* 
	*/
	
	import fl.controls.Button;
	import fl.controls.TextInput;
	import fl.controls.ComboBox;
	import fl.data.DataProvider;
	import flash.events.Event;
	//import flash.utils.Dictionary;
	import flash.utils.*;
	// astra
	import com.yahoo.astra.fl.containers.Form;
	import com.yahoo.astra.containers.formClasses.FormItem;
	import com.yahoo.astra.containers.formClasses.FormLayoutStyle;
	import com.yahoo.astra.events.FormDataManagerEvent;		
	import com.yahoo.astra.fl.utils.FlValueParser;
	import com.yahoo.astra.layout.LayoutContainer;
	import com.yahoo.astra.layout.modes.BoxLayout;
	import com.yahoo.astra.managers.FormDataManager;		
	// src
	import src.components.BForm;
	import src.util.Utils;
	import src.computation.LogicalProbabilisticMethod;
	import src.computation.Simulation;
	import src.computation.SimulationWithRecovery
	
	public class CalcForm extends BForm {
		
		private var time: TextInput;
		private var timeFormItem:FormItem;
		private var methodComboBoxFormItem:FormItem;
		private var methodComboBox:ComboBox;
		private var paramsForm = null;
		private var itemContainer : LayoutContainer;
		private var methods = new Array( 			
			{ text: "Логико-вероятностный метод", className:"src.computation.LogicalProbabilisticMethod" },			
			{ text: "Им. модел. с учетом востановления", className: "src.computation.SimulationWithRecovery"},
			{ text: "Имитационное моделирование", className: "src.computation.Simulation"} );
		private var method;
		
		/**
		 * construct
		 */
		public function CalcForm(){
			super("Выполнить расчет");
			/// added for bug of getDefinitionByName
			var a:LogicalProbabilisticMethod = null;
			var b:Simulation = null;
			var c:SimulationWithRecovery = null;
			//
		}
		
		protected override function initUI() {
			// method
			methodComboBox = new ComboBox();
			methodComboBox.width = 200;
			var dp = new DataProvider();
			for (var i = 0; i < this.methods.length; i++ ) {
				dp.addItem( { label: this.methods[i].text} );
			}			
			methodComboBox.dataProvider = dp;	
			methodComboBox.addEventListener(Event.CHANGE, comboBoxHandler);
			// time
			time = Utils.numberInputText();						
			time.width = 200;
			this.createOkAndCancel();			
		}
		
		protected override function initData() {			
			// method
			methodComboBoxFormItem = new FormItem("Метод расчета", methodComboBox);
			methodComboBoxFormItem.labelAlign = FormLayoutStyle.LEFT;
			methodComboBoxFormItem.labelWidth = 100;
			// time
			timeFormItem = new FormItem("Время", time);
			timeFormItem.labelAlign = FormLayoutStyle.LEFT;
			timeFormItem.labelWidth = 100;
			
			var buttons = new FormItem("", [this.okButton, this.cancelButton]);
			buttons.labelWidth = 100;
			
			var box : BoxLayout = new BoxLayout();
			box.direction = "vertical";
			box.verticalGap = 10;			
			itemContainer = new LayoutContainer(box);	
			itemContainer.autoMask = false;
			itemContainer.x = itemContainer.y = 30;			
			
			itemContainer.addChild(timeFormItem);			
			itemContainer.addChild(methodComboBoxFormItem);
			itemContainer.addChild(buttons);
			this.form.addChild(itemContainer);
		}
		
		private function comboBoxHandler(event){
			var it = this.methods[this.methodComboBox.selectedIndex];			
			if(it.className!=null){
				var ClassReference:Class = getDefinitionByName(it.className) as Class;
				this.method = new ClassReference();
				if (this.paramsForm != null) {
					this.itemContainer.removeChild(this.paramsForm);
				}
				this.paramsForm = method.getParamForm();
				if (this.paramsForm != null) {
					this.itemContainer.addChildAt(this.paramsForm,2);
				}
			}
		}
		
		public override function setData(dict:Dictionary) {}
		
		public override function getData():Dictionary {
			var dict:Dictionary = new Dictionary();
			if (this.method == null) {
				this.method = new LogicalProbabilisticMethod();
			}
			dict["time"] = time.text;
			dict["method"] = this.method;
			return dict;
		}
		
	}
	
}